﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Xml;

namespace ModernWSPView.Pages
{
    public class PageListOfContentTypesModel : INotifyPropertyChanged
    {
        public List<XmlElement> AllFields { get; set; }
        public string ContentTypeName { get; set; }
        public string ContentTypeId { get; set; }
        public string ContentTypeGroup { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public void Load(string ctId)
        {
            this.ContentTypeId = ctId;

            XmlElement xeContentType = AppModel.AppXmlDataSource
                    .Document
                    .SelectSingleNode(string.Format("//ContentType[@ID='{0}']", ctId)) as XmlElement;
            this.ContentTypeName = xeContentType.Attributes["Name"].Value;
            this.ContentTypeGroup = xeContentType.Attributes["Group"].Value;

            AllFields = AppModel.AppXmlDataSource
                    .Document
                    .SelectNodes(string.Format("//ContentType[@ID='{0}']/FieldRefs/FieldRef", ctId))
                    .Cast<XmlElement>()
                    .ToList();

            OnPropertyChanged("AllFields");
            OnPropertyChanged("ContentTypeId");
            OnPropertyChanged("ContentTypeName");
            OnPropertyChanged("ContentTypeGroup");
        }

    }
}
